package handlers

import (
	"GIN-API/global"
	"GIN-API/internal/utils"
	"github.com/gin-gonic/gin"
	"net/http"
)

func UploadImage(c *gin.Context) {
	// 获取上传的图片文件
	file, err := c.FormFile("image")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "上传文件失败"})
		global.Logger.Println("上传文件失败: %v", err)
		return
	}
	// 穷的慌怕上传大了
	if file.Size > 1024*1024*2 {
		c.JSON(http.StatusBadRequest, gin.H{"error": "上传文件大小不能超过2MB"})
		return
	}
	url, err := utils.UploadFile(file)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "上传文件失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"url": url})
}
